package com.icode.web.controller.order;

import com.icode.core.dto.order.OrderDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.house.HouseBase;
import com.icode.core.model.order.Order;
import com.icode.core.shared.DataToJson;
import com.icode.service.house.HouseService;
import com.icode.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lb
 * Date: 15-11-27
 * Time: 下午2:33
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/orderback")
public class OrderBackController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "order_list/{currentPage}", method = RequestMethod.GET)
    public String showOrdersList(@PathVariable String currentPage, Model model) {
        String totalPage = houseService.findTotalPage(Order.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<OrderDTO> orderDTOs = orderService.findAllOrderByPage(currentPage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("orderDTOs", orderDTOs);
        return "/back/order/orderList";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public void showOrderInfoByPhoneNum(String phoneNum, HttpServletResponse response) {
        List<OrderDTO> orderDTOs = orderService.findOrderByUsersInfo(phoneNum);
        DataToJson.orderInfoTojson(orderDTOs, response);
    }
}
